/*
 * 地址选择
 */
'use strict';
import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
    TouchableHighlight,
    Image,
    PixelRatio,
    AsyncStorage,
    BackAndroid,
    TextInput,
} from 'react-native';


import Toast from 'antd-mobile/lib/toast';
import List from 'antd-mobile/lib/list';

import NavTitle from '../component/NavTitle';

import Forget from './Forget';

import { connect } from 'react-redux';
import {changeAddress} from '../action/address';

import Register from './Register';

class Login extends Component {
    constructor(props) {
        super(props)
        this.state = {
            username:void 0,
            password:void 0
        }
        BackAndroid.addEventListener('hardwareBackPress', this.goBack.bind(this));
    }

    componentDidMount(){

    }

    goBack(){

    }

    _forgetPwd(){
        this.props.navigator.push({
            name:'Forget',
            component:Forget
        })
    }

    _register(){
        this.props.navigator.push({
            name:'Register',
            component:Register
        })
    }

    _login(){
        fetch(`http://www.weiweigo.com/app/login.aspx?method=dl`,{
            method:'POST',
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            body:`username=${this.state.username}&userpwd=${this.state.password}`,
            //body:{username:'18288282428',userpwd:'123456'},
        }).then(data=>{
            if (data.ok) {
                data.json().then(json=>{
                    if(json.success){
                        Toast.info(json.msg);
                        AsyncStorage.setItem('serveid',String(json.val.serveId));
                        AsyncStorage.setItem('shopid',String(json.val.shopId),function(){
                            this.props.navigator.pop();
                        }.bind(this))
                    }
                });
            }
        });
    }

    render() {
        return (
            <View style={styles.container}>
                <NavTitle
                    title='登录'
                    headColor = '#fbdc3a'
                />
                <View style={styles.content}>
                    <View style={styles.textInput}>
                        <TextInput
                            style={{height:36,fontSize:14}}
                            placeholder='请输入手机号'
                            onChangeText={(text) => this.setState({username:text})}
                            underlineColorAndroid='transparent'
                            keyboardType = 'default'
                            autoFocus={true}
                        />
                    </View>

                    <View style={styles.textInput}>
                        <TextInput
                            style={{height:36,fontSize:14}}
                            placeholder='请输入密码'
                            keyboardType = 'default'
                            secureTextEntry={true}
                            underlineColorAndroid='transparent'
                            onChangeText={(text) => this.setState({password:text})}
                        />
                    </View>

                    <TouchableHighlight
                        style={styles.loginBtn}
                        onPress = {this._login.bind(this)}
                    >
                        <Text style={styles.loginText}>登录</Text>
                    </TouchableHighlight>

                    <View style={styles.footTip}>
                        <TouchableHighlight
                            style={styles.btn}
                            onPress={this._forgetPwd.bind(this)}
                        >
                            <Text style={styles.tip2}>忘记密码</Text>
                        </TouchableHighlight>
                        <TouchableHighlight
                            style={[styles.btn,{marginLeft:10}]}
                            onPress={this._register.bind(this)}
                        >
                            <Text style={styles.tip2}>注册</Text>
                        </TouchableHighlight>
                    </View>
                </View>
            </View>
        )
    }

}

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#EEEEEF',
        flex: 1,
    },
    itemStyle:{
        flexDirection:'row'
    },
    keys:{
        marginRight:10,
    },
    content:{
      padding:20,
    },
    textInput:{
        padding:5,
        paddingLeft:10,
        borderWidth:1/PixelRatio.get(),
        marginTop:20,
        borderRadius:4,
        borderColor:'#CCC'
    },
    loginBtn:{
        padding:15,
        backgroundColor:'#FBDC3A',
        marginTop:20,
        borderRadius:3,
        justifyContent:'center',
        alignItems:'center'
    },
    loginText:{
        fontSize:16
    },
    footTip:{
        flexDirection:'row',
        justifyContent:'space-between',
        marginTop:10
    },
    btn:{
        borderWidth:1/PixelRatio.get(),
        flex:1,
        alignItems:'center',
        borderColor:'#EB877E',
        borderRadius:2,
        padding:10
    },
    tip1:{
        color:'#c9caca'
    },
    tip2:{
        color:'#e83828',
    }
});

export default Login;
